﻿using AnesSystem.Models;
using AnesSystem.Services;
using Common;
using Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AnesSystem.Util
{
    class WorkScoreUtil
    {

        public static GongFenZhiBiaoRCModel GetWorkZhiBiao(PatientLinkModel patientLink)
        {

            GongFenZhiBiaoRCModel gongFenZhiBiaoRCModel = new GongFenZhiBiaoRCModel();

            if (patientLink != null)
            {

                bool hasJieBanDoc = false;
                if (patientLink.AnesthesiaRecord.Mzjbys != null
                        && patientLink.AnesthesiaRecord.Mzjbys.Length >= 0)
                {
                    hasJieBanDoc = true;
                }
                //手术时长
                long timeGap = 0;
                DateTime mazuiStartTime = patientLink.Status.InRoomTime;
                DateTime mazuiEndTime = patientLink.Status.OutRoomTime;
                if (mazuiStartTime != null && mazuiEndTime != null)
                {
                    timeGap = (long)(mazuiEndTime - mazuiStartTime).TotalSeconds;
                }
                //接班时间时长
                long jieBanTimeGap = 0;
                DateTime jieBanTime = Convert.ToDateTime(patientLink.AnesthesiaRecord.MzjbysStartTime);
                if (null != jieBanTime && null != mazuiEndTime)
                {
                    jieBanTimeGap = (long)(mazuiEndTime - jieBanTime).TotalSeconds;
                }
                if (0 == jieBanTimeGap) jieBanTimeGap = 0;

                String complicatedMethod = patientLink.AnesthesiaRecord.Mzff;
                //麻醉方式
                if (null != patientLink.AnesthesiaRecord.Mzff)
                {
                    if (complicatedMethod == null && complicatedMethod.Equals(""))
                    {  //无麻醉
                    }
                    else if (complicatedMethod.Contains("气管插管全麻(加强)")
                          || complicatedMethod.Contains("静脉麻醉") && complicatedMethod.Contains("复合"))
                    { //全麻单腔、静脉全麻
                        gongFenZhiBiaoRCModel.QuanMaDanQiang = (int)(gongFenZhiBiaoRCModel.QuanMaDanQiang + getScore(hasJieBanDoc, timeGap, jieBanTimeGap, 4));
                    }
                    else if (complicatedMethod.Contains("气管插管全麻(双腔)"))
                    {  //全麻双腔
                        gongFenZhiBiaoRCModel.QuanMaShuangQiang = (int)(gongFenZhiBiaoRCModel.QuanMaShuangQiang + getScore(hasJieBanDoc, timeGap, jieBanTimeGap, 6));
                    }
                    else if (complicatedMethod.Contains("腰硬联合麻醉") && complicatedMethod.Contains("复合"))
                    {  //腰硬联合麻醉
                        gongFenZhiBiaoRCModel.YaoYingLianHe = (int)(gongFenZhiBiaoRCModel.YaoYingLianHe + getScore(hasJieBanDoc, timeGap, jieBanTimeGap, 4));
                    }
                    else if (complicatedMethod.Contains("椎管内") || complicatedMethod.Contains("硬膜外") || complicatedMethod.Contains("分娩") && complicatedMethod.Contains("复合"))
                    { //硬膜外麻醉
                        gongFenZhiBiaoRCModel.YingMoWai = (int)(gongFenZhiBiaoRCModel.YingMoWai + getScore(hasJieBanDoc, timeGap, jieBanTimeGap, 3));
                    }
                    else if (complicatedMethod.Contains("神经"))
                    { //神经阻滞麻醉
                        gongFenZhiBiaoRCModel.ShenJingZuZhi = (int)(gongFenZhiBiaoRCModel.ShenJingZuZhi + getScore(hasJieBanDoc, timeGap, jieBanTimeGap, 3));
                    }
                    else if (complicatedMethod.Contains("复合"))
                    { //复合麻醉
                        gongFenZhiBiaoRCModel.QuanMaDanQiang = (int)(gongFenZhiBiaoRCModel.QuanMaDanQiang + getScore(hasJieBanDoc, timeGap, jieBanTimeGap, 4));
                        gongFenZhiBiaoRCModel.YingMoWai = (int)(gongFenZhiBiaoRCModel.YingMoWai + getScore(hasJieBanDoc, timeGap, jieBanTimeGap, 3));
                    }
                    else if (complicatedMethod.Contains("复合") && complicatedMethod.Contains("插管") && complicatedMethod.Contains("静脉"))
                    {
                        gongFenZhiBiaoRCModel.QuanMaDanQiang = (int)(gongFenZhiBiaoRCModel.QuanMaDanQiang + getScore(hasJieBanDoc, timeGap, jieBanTimeGap, 4));
                        gongFenZhiBiaoRCModel.ShenJingZuZhi = (int)(gongFenZhiBiaoRCModel.ShenJingZuZhi + getScore(hasJieBanDoc, timeGap, jieBanTimeGap, 3));
                    }
                    else if (complicatedMethod.Contains("复合") && complicatedMethod.Contains("非插管"))
                    {
                        gongFenZhiBiaoRCModel.YingMoWai = (int)(gongFenZhiBiaoRCModel.YingMoWai
                                + getScore(hasJieBanDoc, timeGap, jieBanTimeGap, 3));
                    }
                    else if (complicatedMethod.Contains("复合") && complicatedMethod.Contains("非插管") && complicatedMethod.Contains("静脉"))
                    {
                        gongFenZhiBiaoRCModel.ShenJingZuZhi = (int)(gongFenZhiBiaoRCModel.ShenJingZuZhi
                                + getScore(hasJieBanDoc, timeGap, jieBanTimeGap, 3));
                    }
                    else if (complicatedMethod.Contains("复合") && complicatedMethod.Contains("椎管内") && complicatedMethod.Contains("静脉"))
                    {
                        gongFenZhiBiaoRCModel.YingMoWai = (int)(gongFenZhiBiaoRCModel.YingMoWai
                                + getScore(hasJieBanDoc, timeGap, jieBanTimeGap, 3));
                        gongFenZhiBiaoRCModel.ShenJingZuZhi = (int)(gongFenZhiBiaoRCModel.ShenJingZuZhi
                                + getScore(hasJieBanDoc, timeGap, jieBanTimeGap, 3));
                    }
                }

                //中心静脉穿刺
                if (patientLink.AnesthesiaRecord.Zxjmcc)
                {
                    gongFenZhiBiaoRCModel.ZhongXinJingMaiChuanCi = (int)(gongFenZhiBiaoRCModel.ZhongXinJingMaiChuanCi
                            + getScore(hasJieBanDoc, timeGap, jieBanTimeGap, 3));
                }

                //动脉穿刺
                if (patientLink.AnesthesiaRecord.Dmcc)
                {
                    gongFenZhiBiaoRCModel.DongMaiChuanCi = (int)(gongFenZhiBiaoRCModel.DongMaiChuanCi
                            + getScore(hasJieBanDoc, timeGap, jieBanTimeGap, 3));
                }

                //术中血气分析
                if (patientLink.AnesthesiaRecord.Xqfx)
                {
                    String numStr = patientLink.AnesthesiaRecord.Xqfxnr;
                    try
                    {
                        int num = int.Parse(numStr);
                        gongFenZhiBiaoRCModel.ShuZhongXueQiFenXi = (int)(gongFenZhiBiaoRCModel.ShuZhongXueQiFenXi
                                + getScore(hasJieBanDoc, timeGap, jieBanTimeGap, num));
                    }
                    catch (Exception ex)
                    {
                    }
                }

                //术后镇痛
                if (patientLink.AnesthesiaRecord.PCIA || patientLink.AnesthesiaRecord.PCEA
                        || patientLink.AnesthesiaRecord.PCNA)
                {
                    gongFenZhiBiaoRCModel.ShuHouZhenTong = (int)(gongFenZhiBiaoRCModel.ShuHouZhenTong
                            + getScore(hasJieBanDoc, timeGap, jieBanTimeGap, 1));
                }

                if (timeGap >= 0 && null != complicatedMethod && !complicatedMethod.Equals(""))
                {
                    int hour = (int)(timeGap / Constants.ONE_HOUR_LONG) + (timeGap % Constants.ONE_HOUR_LONG == 0 ? 0 : 1);
                    if (complicatedMethod.Contains("气管插管") || complicatedMethod.Contains("复合")
                            || complicatedMethod.Contains("静脉"))
                    { //全麻、复合麻醉，静脉全麻
                        gongFenZhiBiaoRCModel.QuanMaJianCe = (int)(gongFenZhiBiaoRCModel.QuanMaJianCe
                                + getScore(hasJieBanDoc, timeGap, jieBanTimeGap, hour * 3));
                    }
                    else
                    {
                        if (patientLink.Patient.Ksmc != null &&
                                patientLink.Patient.Ksmc.Equals("外三科"))
                        {
                            float score = (hour - 3 > 0 ? (hour - 3) * 3 + 3 * 2 : hour * 2);
                            score = getScore(hasJieBanDoc, timeGap, jieBanTimeGap, score);
                            gongFenZhiBiaoRCModel.FeiQuanMaJianCe = (int)(gongFenZhiBiaoRCModel.FeiQuanMaJianCe + score);
                        }
                        else
                        {
                            gongFenZhiBiaoRCModel.FeiQuanMaJianCe = (int)(gongFenZhiBiaoRCModel.FeiQuanMaJianCe
                                    + getScore(hasJieBanDoc, timeGap, jieBanTimeGap, hour * 2));
                        }
                    }
                }

            }

            return gongFenZhiBiaoRCModel;
        }

        public static void CalculateScoreDetails(PatientLinkModel patientLink)
        {
            GongFenZhiBiaoRCModel gongFenZhiBiaoRCModel = patientLink.Gongfen;
            if (patientLink != null)
            {
                //手术时长
                long timeGap = 0;
                int per = 0;
                DateTime inRoomTime = patientLink.Status.InRoomTime;

                DateTime outRoomTime = patientLink.Status.OutRoomTime;
                if (inRoomTime != new DateTime() && outRoomTime != new DateTime())
                {
                    timeGap = GetTimeSpan(inRoomTime, outRoomTime);
                }
                else
                {
                    return;
                }
                per = (int)timeGap / 30;
                if (timeGap % 30 != 0)
                {
                    per = per + 1;
                }

                ////接班时间时长
                //long jieBanTimeGap = 0;
                //DateTime jieBanTime = Convert.ToDateTime(patientLink.AnesthesiaRecord.MzjbysStartTime);
                //if (null != jieBanTime && null != outRoomTime)
                //{
                //    jieBanTimeGap = (long)(outRoomTime - jieBanTime).TotalSeconds;
                //}
                //if (0 == jieBanTimeGap) jieBanTimeGap = 0;

                string complicatedMethod = patientLink.AnesthesiaRecord.Mzff;
                //麻醉方式
                if (null != patientLink.AnesthesiaRecord.Mzff)
                {
                    if (complicatedMethod == null && complicatedMethod.Equals(""))
                    {  //无麻醉
                    }
                    else if (complicatedMethod.Contains("气管插管全麻"))
                    {
                        gongFenZhiBiaoRCModel.QuanMaDanQiang = 2 * per;
                    }
                    else if (complicatedMethod.Contains("椎管内麻醉"))
                    {
                        gongFenZhiBiaoRCModel.YingMoWai = 1 * per;
                    }
                    else if (complicatedMethod.Contains("神经"))
                    {
                        gongFenZhiBiaoRCModel.ShenJingZuZhi = 1 * per;
                    }
                    else if (complicatedMethod.Contains("局麻"))
                    {
                        gongFenZhiBiaoRCModel.JuBuMaZui = 0.5 * per;
                    }
                }
                //术后镇痛
                if (patientLink.AnesthesiaRecord.PCIA || patientLink.AnesthesiaRecord.PCEA || patientLink.AnesthesiaRecord.PCNA)
                {
                    gongFenZhiBiaoRCModel.ShuHouZhenTong = 2;
                }
                //中心静脉穿刺
                if (patientLink.AnesthesiaRecord.Zxjmcc)
                {
                    gongFenZhiBiaoRCModel.ZhongXinJingMaiChuanCi = 2;
                }
                //年龄
                double age = patientLink.Patient.Nl.Trim().Replace("岁", "").ToDouble();
                if (age < 6)
                {
                    gongFenZhiBiaoRCModel.NianLingXiaoYuLiu = 1 * per;
                }
                else if (age > 6 && age <= 12)
                {
                    gongFenZhiBiaoRCModel.NianLingXiaoYuShier = 0.5 * per;
                }
                else if (age >= 70 && age < 80)
                {
                    gongFenZhiBiaoRCModel.NianLingDaYuQiShi = 0.5 * per;
                }
                else if (age >= 80 && age < 90)
                {
                    gongFenZhiBiaoRCModel.NianLingDaYuBaShi = 1 * per;
                }
                else if (age >= 90)
                {
                    gongFenZhiBiaoRCModel.NianLingDaYuJiuShi = 1.5 * per;
                }

                List<PatientEventsModel> listevents = PatientEventsService.Instance.GetPatientEvents8Id(patientLink.Id.ToString());
                foreach (var item in listevents)
                {
                    if (item.Code.Contains("有创"))
                    {
                        gongFenZhiBiaoRCModel.YouChuangXueYaJianCe += 1 * per;
                    }
                    else if (item.Code.Contains("麻醉深度"))
                    {
                        gongFenZhiBiaoRCModel.MaZuiShenDuJianCe += 1 * per;
                    }
                }
            }
        }

        private static float getScore(bool hasJieBanDoc, long mazuiTimeGap, long jieBanTimeGap, float srcScore)
        {
            if (!hasJieBanDoc) return srcScore;
            float jieBanRate = jieBanTimeGap / mazuiTimeGap;
            float jieBanScore = (float)(Math.Round(srcScore * jieBanRate * 10)) / 10;
            float normalScore = srcScore - jieBanScore;
            return normalScore;
        }

        public static long GetTimeSpan(DateTime d1, DateTime d2)
        {
            long spanTime = 0;
            TimeSpan span = d2 - d1;
            long Minutes = span.Days * 24 * 60 + span.Hours * 60 + span.Minutes;
            if (Minutes > 0)
            {
                spanTime = Minutes;
            }
            return spanTime;
        }
    }
}
